package org.unique.generator;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.apache.commons.lang.StringUtils;

/**
 * 字符串处理工具类
 * 
 * @ClassName: StringUtil
 * @author Andrew.Wen
 * @date 2013-1-10 下午3:36:22
 */
public class StringUtil {

	/**
	 * 获得类首字母小写
	 * 
	 * @param className
	 * @return
	 */
	public static String getClassLower(String className) {
		return className.substring(0, 1).toLowerCase() + className.substring(1);
	}

	/**
	 * 获得类首字母大写
	 * 
	 * @param className
	 * @return
	 */
	public static String getClassUpper(String className) {
		return className.substring(0, 1).toUpperCase() + className.substring(1);
	}

	/**
	 * 数据库字段格式化后变小写
	 * @param field
	 * @return
	 */
	public static String formatField(String field) {
		String[] strs = field.split("_");
		field = "";
		int m = 0;
		for (int length = strs.length; m < length; ++m) {
			if (m > 0) {
				String tempStr = strs[m].toLowerCase();
				tempStr = tempStr.substring(0, 1).toUpperCase()
						+ tempStr.substring(1, tempStr.length());
				field = field + tempStr;
			} else {
				field = field + strs[m].toLowerCase();
			}
		}
		return field;
	}
	
	/**
	 * 数据库字段格式化后变大写
	 * @param field
	 * @return
	 */
	public static String formatFieldCapital(String field) {
		String[] strs = field.split("_");
		field = "";
		int m = 0;
		for (int length = strs.length; m < length; ++m) {
			if (m > 0) {
				String tempStr = strs[m].toLowerCase();
				tempStr = tempStr.substring(0, 1).toUpperCase()
						+ tempStr.substring(1, tempStr.length());
				field = field + tempStr;
			} else {
				if(strs[0].indexOf("Impl")<0){
					field = field + strs[0].toLowerCase();
				}else{
					field= strs[0];
				}
			}
		}
		field = field.substring(0, 1).toUpperCase() + field.substring(1);
		return field;
	}
	
	/**
	 * 获得set 方法
	 * 
	 * @Title: getSetMethod
	 * @param field
	 *            字段名称
	 * @return
	 */
	public static String getSetMethod(String field) {
		String methodStr = "";

		methodStr = "set";

		if ((field == null) || (field == "")) {
			return "";
		} else {
			field = methodStr + field.substring(0, 1).toUpperCase()
					+ field.substring(1);

			return field;
		}
	}

	/**
	 * 获得get 方法
	 * 
	 * @Title: getGetMethod
	 * @param field
	 *            字段名称
	 * @return
	 */
	public static String getGetMethod(String field) {
		String methodStr = "";

		methodStr = "get";

		if ((field == null) || (field == "")) {
			return "";
		} else {
			field = methodStr + field.substring(0, 1).toUpperCase()
					+ field.substring(1);

			return field;
		}
	}

	/**
	 * 去掉下划线，将下划线后首字母大写
	 * 
	 * @Title: getDomainColumnName
	 * @param databaseColumn
	 * @return
	 */
	public static String getDomainColumnName(String databaseColumn) {

		if ((databaseColumn == null) || (databaseColumn == "")) {
			return "";
		} else {

			int _postion = -1;

			while (databaseColumn.indexOf("_") > 0) {
				_postion = databaseColumn.indexOf("_");
				if (_postion < databaseColumn.length() - 1) {
					databaseColumn = databaseColumn.substring(0, _postion)
							+ databaseColumn.substring(_postion + 1,
									_postion + 2).toUpperCase()
							+ databaseColumn.substring(_postion + 2,
									databaseColumn.length());
				} else {
					databaseColumn = databaseColumn.replace("_", "");
				}
			}

		}

		return databaseColumn;

	}

	public static String getColumnType(int databaseType) {
		String colType = "";
		
		switch (databaseType) {
		case java.sql.Types.DECIMAL:
		case java.sql.Types.REAL:
			colType = "Float";
			break;
		
		case java.sql.Types.INTEGER:
		case java.sql.Types.BIGINT:
		case java.sql.Types.TINYINT:
		
			colType = "Integer";
			break;
		case java.sql.Types.VARCHAR:
		case java.sql.Types.CHAR:
		case java.sql.Types.LONGVARBINARY:
		case java.sql.Types.LONGVARCHAR:
			colType = "String";
			break;
			
		case java.sql.Types.DATE:
		case java.sql.Types.TIMESTAMP:
			colType="Date";
			break;
		default:
			System.out.println("找不到数据类型："+databaseType);
			break;
		}

		return colType;
	}

	/**
	 * 数据库类型转成java完整类名
	 * @param dataType
	 * @param precision
	 * @param scale
	 * @return
	 */
	public static String formatDataType(String dataType, String precision,String scale) {
		dataType=dataType.toLowerCase();
		if (dataType.contains("char") || dataType.contains("text"))
			dataType = "java.lang.String";
		else if (dataType.contains("int"))
			dataType = "java.lang.Integer";
		else if (dataType.contains("float"))
			dataType = "java.lang.Float";
		else if (dataType.contains("double"))
			dataType = "java.lang.Double";
		else if (dataType.contains("number")) {
			if ((StringUtils.isNotBlank(scale))
					&& (Integer.parseInt(scale) > 0))
				dataType = "java.math.BigDecimal";
			else if ((StringUtils.isNotBlank(precision))
					&& (Integer.parseInt(precision) > 10))
				dataType = "java.lang.Long";
			else
				dataType = "java.lang.Integer";
		} else if (dataType.contains("decimal"))
			dataType = "BigDecimal";
		else if (dataType.contains("date"))
			dataType = "java.util.Date";
		else if (dataType.contains("time")) {
			dataType = "java.util.Date";
		} else if (dataType.contains("blob"))
			dataType = "byte[]";
		else if (dataType.contains("clob"))
			dataType = "java.sql.Clob";
		else if (dataType.contains("numeric"))
			dataType = "BigDecimal";
		else {
			dataType = "java.lang.Object";
		}
		if(dataType.lastIndexOf(".")>0){
			dataType=dataType.substring(dataType.lastIndexOf(".")+1,dataType.length());
		}
		return dataType;
	}
	
	/**
	 * 从属性文件获取配置
	 * @param filename
	 * @param key
	 * @return
	 * @throws FileNotFoundException 
	 */
	public synchronized static String getPropertyFromFile(String filename, String key) 
    {

		String paodingAnalysisPath = System.getProperty("user.dir") + "\\"+filename;
		
		InputStream in1=null;
		ResourceBundle rb=null;
		try {
			in1 = new BufferedInputStream(new FileInputStream(paodingAnalysisPath));
			
			   rb = new PropertyResourceBundle(in1);
			   
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//ResourceBundle resourceBundle1 = new PropertyResourceBundle(in1);
		
		
     

        return rb.getString(key).trim();

    }
	
	public static String date2String(){ 
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); 
		return sdf.format(new Date()); 
	}
}
